package cn.tedu._03mvcboot2.controller;

import cn.tedu._03mvcboot2.mapper.UserMapper;
import cn.tedu._03mvcboot2.pojo.dto.UserDTO;
import cn.tedu._03mvcboot2.pojo.entity.User;
import cn.tedu._03mvcboot2.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
/*  RestController 将类中方法返回值直接作为HTTP响应的内容
    RestController=Controller+ResponseBody注解组合

    @RequestMapping("/v1/users/") 填写相同部分的地址
    v1/car v1/shooping
* */
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("insert")
    public String insert(@RequestBody UserDTO userDTO) {
        User user = new User();
//        user.setUsername(userDTO.getUsername());
//        user.setPassword(userDTO.getPassword());
//        user.setNickname(userDTO.getNickname());
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return "添加成功";
    }
    //  查询所有用户,显示用户名和密码
    //RESTful风格的API
    //@RequestMapping(value = "/v1/users/userList",method=RequestMethod.GET)
    //限定请求方式的注解  @GetMapping，@PostMapping
    //POST请求测试GetMapping状态码405
    @GetMapping("userList")
    public List<UserListVO> userList() {
        return userMapper.selectUserList();
    }

    //@PathVariable 可以将URL中的变量映射到Controller中的方法参数上
    @GetMapping("delete")
    @ResponseBody
    public String delete(int id){
        userMapper.deleteUserById(id);
        return "删除成功";
    }

    @PostMapping("update")
    public String update(User user){
        userMapper.updateUserById(user);
        return "修改成功";
    }

}
